package com.burst.reptile.modular.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.VerifyCodeUtils;
import com.burst.reptile.common.utils.sms.SendSms;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.redis.RedisCache;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.entity.SysUser;
import com.burst.reptile.modular.system.service.ISysUserService;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 验证码操作处理
 * @author wujin
 * @date: 2020年5月22日 下午4:20:12
 */
@RestController
public class CaptchaController {
	
	@Autowired
	private RedisCache redisCache;
	
	@Autowired
	private ISysUserService userService;

	/**
	 * 生成验证码
	 */
	@GetMapping("/captchaImage")
	public Result<Object> getCode(HttpServletResponse response) throws IOException {
		// 生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		// 唯一标识
		String uuid = IdUtil.simpleUUID();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;

		redisCache.setCacheObject(verifyKey, verifyCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		// 生成图片
		int w = 111, h = 36;
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		VerifyCodeUtils.outputImage(w, h, stream, verifyCode);
		try {
			Map<String, String> map = new HashMap<>();
			map.put("uuid", uuid);
			map.put("img", Base64.encode(stream.toByteArray()));
			return Result.success(map);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		} finally {
			stream.close();
		}
	}
	
	/**
	 * 登录发送短信验证码
	 * @return
	 */
	@GetMapping("/code/sms")
	@Log(title = "登录发送短信验证码", businessType = BusinessType.INSERT)
	public Result<Object> sendCode(@RequestParam(required = false) String phone){
		if(StrUtil.isEmpty(phone)) {
			return Result.error("手机号不能为空");
		}
		if(!Validator.isMobile(phone)) {
			return Result.error("手机号格式不正确");
		}
		// 唯一标识
		String uuid = IdUtil.simpleUUID();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String randomNumbers = RandomUtil.randomNumbers(4);
		System.out.println("登录校验,验证码：" + randomNumbers);
//		SendSms.send("登录校验", randomNumbers, phone);
		SendSms.sendSdk(randomNumbers, "668098", phone);
		redisCache.setCacheObject(verifyKey, randomNumbers, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		return Result.success(uuid);
	}
	
	/**
	 * 注册发送短信验证码
	 * @return
	 */
	@GetMapping("/register/sms")
	@Log(title = "注册发送短信验证码", businessType = BusinessType.INSERT)
	public Result<Object> registerCode(@RequestParam(required = false) String phone){
		if(StrUtil.isEmpty(phone)) {
			return Result.error("手机号不能为空");
		}
		if(!Validator.isMobile(phone)) {
			return Result.error("手机号格式不正确");
		}
		// 唯一标识
		String uuid = IdUtil.simpleUUID();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String randomNumbers = RandomUtil.randomNumbers(4);
		System.out.println("注册校验,验证码：" + randomNumbers);
//		SendSms.send("注册校验", randomNumbers, phone);
		SendSms.sendSdk(randomNumbers, "668099", phone);
		redisCache.setCacheObject(verifyKey, randomNumbers, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		return Result.success(uuid);
	}
	
	/**
	 * 重置密码发送短信验证码
	 * @return
	 */
	@GetMapping("/reset/sms")
	@Log(title = "重置密码发送短信验证码", businessType = BusinessType.INSERT)
	public Result<Object> reset(){
		SysUser byId = userService.getById(SecurityUtils.getLoginUserId());
		// 唯一标识
		String uuid = IdUtil.simpleUUID();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String randomNumbers = RandomUtil.randomNumbers(4);
		System.out.println("重置密码校验,验证码：" + randomNumbers);
//		SendSms.send("重置密码校验", randomNumbers, byId.getPhonenumber());
		SendSms.sendSdk(randomNumbers, "668100", byId.getPhonenumber());
		redisCache.setCacheObject(verifyKey, randomNumbers, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		return Result.success(uuid);
	}
	
	/**
	 * 找回密码发送短信验证码
	 * @return
	 */
	@Log(title = "找回密码发送短信验证码", businessType = BusinessType.INSERT)
	@GetMapping("/newforgot/sms")
	public Result<Object> newforgot(@RequestParam(required = false) String phone){
		if(StrUtil.isEmpty(phone)) {
			return Result.error("手机号不能为空");
		}
		if(!Validator.isMobile(phone)) {
			return Result.error("手机号格式不正确");
		}
		// 唯一标识
		String uuid = IdUtil.simpleUUID();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String randomNumbers = RandomUtil.randomNumbers(4);
		System.out.println("找回密码校验,验证码：" + randomNumbers);
//		SendSms.send("找回密码校验", randomNumbers, phone);
		SendSms.sendSdk(randomNumbers, "668101", phone);
		redisCache.setCacheObject(verifyKey, randomNumbers, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		return Result.success(uuid);
	}
	
	/**
	 * 帮助找铺 转铺 买铺发送短信验证码
	 * @return
	 */
	@Log(title = "帮助找铺、转铺、买铺发送短信验证码", businessType = BusinessType.INSERT)
	@GetMapping("/fast/sms")
	public Result<Object> helpsms(@RequestParam(required = false) String phone){
		if(StrUtil.isEmpty(phone)) {
			return Result.error("手机号不能为空");
		}
		if(!Validator.isMobile(phone)) {
			return Result.error("手机号格式不正确");
		}
		// 唯一标识
		String uuid = IdUtil.simpleUUID();
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String randomNumbers = RandomUtil.randomNumbers(4);
		System.out.println("短信验证,验证码：" + randomNumbers);
		SendSms.send("短信验证", randomNumbers, phone);
		redisCache.setCacheObject(verifyKey, randomNumbers, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		return Result.success(uuid);
	}
	
}
